package com.didd.core.dubbo.file;

import com.didd.core.common.base.ResultCode;
import com.didd.core.common.base.RpcResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.Objects;

/**
 * 业务返回null,组装公共response
 *
 * @author diddd
 * @date 2019/07/10
 */
@Slf4j
@Activate(group = Constants.PROVIDER, order = 2)
public class ResponseNullFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Result result = invoker.invoke(invocation);
        if (Objects.isNull(result.getValue())) {
            RpcResponse response = RpcResponse.fail(ResultCode.SUCCESS);
            return new RpcResult(response);
        }
        return result;
    }
}
